package com.qf.shop.dao.Impl;

import com.qf.shop.dao.GoodsDao;
import com.qf.shop.entity.Goods;
import com.qf.shop.entity.Page;
import com.qf.shop.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsDaoImpl implements GoodsDao {
    private QueryRunner queryRunner =new QueryRunner();
    @Override
    public List<Goods> selectAllByPage(Page page, int typeid) {
        try {
            System.out.println(page.getStartRows());
            System.out.println(page.getPageSize());
            System.out.println(typeid);
            return queryRunner.query(DruidUtils.getConnection(),"select * from goods where typeid=? limit ?,? ",new BeanListHandler<>(Goods.class),typeid,page.getStartRows(),page.getPageSize());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long selectCount(int typeid) {
        try {
            return  queryRunner.query(DruidUtils.getConnection(),"select count(*) from goods where typeid = ?;",new ScalarHandler<>(),typeid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Goods selectGoods(int id) {
        try {
            return queryRunner.query(DruidUtils.getConnection(),"select * from goods where id= ?;",new BeanHandler<>(Goods.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
